package com.fanshuai.soring.boot.config;

import com.fanshuai.soring.boot.mvc.log.LogProperties;
import com.fanshuai.soring.boot.mvc.log.SimpleLogFilter;
import com.google.common.collect.ImmutableList;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;

@EnableConfigurationProperties(value = LogProperties.class)
public class RequestLogConfig {
    @Bean
    public FilterRegistrationBean<SimpleLogFilter> logFilterFilterRegistrationBean(LogProperties logProperties) {
        FilterRegistrationBean<SimpleLogFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new SimpleLogFilter(logProperties));

        filterFilterRegistrationBean.setUrlPatterns(ImmutableList.of("/*"));
        filterFilterRegistrationBean.setName("SimpleLogFilter");
        filterFilterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        return filterFilterRegistrationBean;
    }
}
